<?php

class Vendors extends Zend_Db_Table_Abstract
{
    protected $_name = 'vendors';
	
	private $_errCode = '';
	private $_errStr = '';
	
	public function errCode() { return $this->_errCode; }
	public function errStr() { return $this->_errStr; }
	
	public function getCaterersForBranch( $branch_id )
	{
		// $this->fetchAll("branch_id='$branch_id' and type='CATERER'")
		$caterers = array();
		
		if(!isset($branch_id))
		{
			$this->_errCode = "PARAM_ERR";
			$this->_errStr = "Missing branch id";
			return null;
		}
		
		try
		{
			$r = $this->fetchAll("branch_id='$branch_id' and type='CATERER'");
			foreach($r as $rrow)
				$caterers[] = $rrow->toArray();
		}
		catch(Exception $e)
		{
			$this->_errCode = "QUERY_ERR";
			$this->_errStr = "Exception:".$e;
			return null;
		}
		
		return $caterers;
	}
	
	public function setCaterersForBranch( &$caterers, $branch_id )
	{
		if(!isset($branch_id))
		{
			$this->_errCode = "PARAM_ERR";
			$this->_errStr = "Missing branch id";
			return null;
		}
		
		if(!isset($caterers))
		{
			$this->_errCode = "PARAM_ERR";
			$this->_errStr = "Missing caterers array";
			return null;
		}
		
		try
		{
			$this->delete("branch_id='$branch_id' and type='CATERER'");
		}
		catch(Exception $e)
		{
			$this->_errCode = "QUERY_ERR";
			$this->_errStr = "Exception".$e;
			return null;
		}
		
		// multiple $this->insert()s with CATERER as the type
		foreach( $caterers as &$caterer )
		{
			// best thing to do is create another array to insert from so we can
			// skip the menus array reference I'll have to add to keep them together...
			$caterer_to_insert = array();
			
			$caterer_to_insert['type'] = 'CATERER';
			$caterer_to_insert['branch_id'] = $branch_id;
			$caterer_to_insert['locale_id'] = $caterer['locale_id'];
						
			$caterer_to_insert['name'] = $caterers['name'];
			$caterer_to_insert['address'] = $caterers['address'];
			$caterer_to_insert['city'] = $caterers['city'];
			$caterer_to_insert['state'] = $caterers['state'];
			$caterer_to_insert['zip'] = $caterers['zip'];
			$caterer_to_insert['phone'] = $caterers['phone'];
			$caterer_to_insert['fax'] = $caterers['fax'];
			$caterer_to_insert['email'] = $caterers['email'];
			$caterer_to_insert['contact'] = $caterers['contact'];
			$caterer_to_insert['url'] = $caterers['url'];
				
			$this->insert( $caterer_to_insert );
			
			$caterer['id'] = $this->last_insert_id();
		}
		
		return $caterers;
	}
	
	public function getFloristsForBranch( $branch_id )
	{
		$florists = array();
		
		if(!isset($branch_id))
		{
			$this->_errCode = "PARAM_ERR";
			$this->_errStr = "Missing branch id";
			return null;
		}
		
		try
		{
			$r = $this->fetchAll("branch_id='$branch_id' and type='FLORIST'");
			foreach($r as $rrow)
				$florists[] = $rrow->toArray();
		}
		catch(Exception $e)
		{
			$this->_errCode = "QUERY_ERR";
			$this->_errStr = "Exception:".$e;
			return null;
		}
		
		return $florists;
	}
	
	public function setFloristsForBranch( &$florists, $branch_id )
	{
		// $this->delete("branch_id='$branch_id' and type='FLORIST'")
		// multiple $this->insert()s with FLORIST as the type
	}	
}

?>
